﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AyFrame.Common
{
    /// <summary>
    /// 层级id类
    /// </summary>
    public class LevelStep
    {
        /// <summary>
        /// 一级
        /// </summary>
        public const long LEVEL_ONE = 0b00000000000100000000000000000000;  // 1048576

        /// <summary>
        /// 二级
        /// </summary>
        public const long LEVEL_TWO = 0b00000000000000000001000000000000;  //4096

        /// <summary>
        /// 三级
        /// </summary>
        public const long LEVEL_THREE = 0b00000000000000000000000000000001;

        /// <summary>
        /// 超级最大值
        /// </summary>
        public const long LEVELRANGE_ZERO = 0b00001111111111111111111111111111;  //268435455
        /// <summary>
        /// 一级最大值
        /// </summary>
        public const long LEVELRANGE_ONE = 0b00000000000011111111111111111111;  //1048575
        /// <summary>
        /// 二级最大值
        /// </summary>
        public const long LEVELRANGE_TWO = 0b00000000000000000000111111111111;  // 4095
        /// <summary>
        /// 三级最大值
        /// </summary>
        public const long LEVELRANGE_THREE = 0;

        /// <summary>
        /// 获取层级步长
        /// </summary>
        /// <param name="level">1，2，3</param>
        /// <returns></returns>
        public static long GetStep(byte level)
        {
            long step = 0;

            switch (level)
            {
                case 0:
                    step = LEVEL_ONE;
                    break;
                case 1:
                    step = LEVEL_TWO;
                    break;
                case 2:
                    step = LEVEL_THREE;
                    break;
                default:
                    break;
            }

            return step;
        }

        /// <summary>
        /// 获取id所在层级下一级最大id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static long GetMaxId(long id)
        {
            long step = LEVELRANGE_THREE;

            if ((id & LEVELRANGE_ZERO) == 0)
            {
                step = LEVELRANGE_ZERO;
            }
            else if ((id & LEVELRANGE_ONE) == 0)
            {
                step = LEVELRANGE_ONE;
            }
            else if ((id & LEVELRANGE_TWO) == 0)
            {
                step = LEVELRANGE_TWO;
            }

            return id + step;
        }
    }
}
